<?php

class BackOrderController extends BackController {

    static $model_name = 'order';
    static $display_name = '订单';
    /*
    |--------------------------------------------------------------------------
    | Default Home Controller
    |--------------------------------------------------------------------------
    |
    | You may wish to use controllers instead of, or in addition to, Closure
    | based routes. That's great! Here is an example controller method to
    | get you started. To route to this controller, just add the route:
    |
    |   Route::get('/', 'HomeController@showWelcome');
    |
    */

    public function getIndex()
    {
        $result =  $this->getService()->getList();
        $entries = $result['entries'];
        $links = $result['links'];
        $status_for_display = $result['status_for_display'];
        $payment_type_display = $result['payment_type_display'];
        $sum = $result['sum'];

        return View::make(self::$view_path.'.'.self::$model_name.'.index', compact('entries', 'links', 'status_for_display','payment_type_display','sum'));
    }

    /**
     * 今日配送订单
     * @return string
     */
    public function getTodaySendList(){
        $result =  $this->getService()->getList(array(
            'status' => Order::getStatusIDByKeyword(Order::SETTLED),
            'send_meal_time_start' => date('Y-m-d',time()),
            'send_meal_time_end' => date('Y-m-d',time()),
            'order' => 'send_meal_time',
        ), 50, false);

        return json_encode(array('result'=>true, 'entries'=>$result));
    }

    /**
     * 今日取餐订单
     * @return string
     */
    public function getTodayGetList(){
        $result =  $this->getService()->getList(array(
            'status' => Order::getStatusIDByKeyword(Order::SETTLED),
            'get_meal_time_start' => date('Y-m-d',time()),
            'get_meal_time_end' => date('Y-m-d',time()),
            'order' => 'get_meal_time',
        ), 50);
        $entries = $result['entries'];

        return json_encode(array('result'=>true, 'entries'=>$entries));
    }

    public function getDetail($order){
        return View::make(self::$view_path.'.'.self::$model_name.'.detail', compact('order'));
    }

    public function doExport(){
        $service = $this->getService();
        $result = $service->exportFood();
        if($result === false){
            return Redirect::action('BackFoodController@getIndex')->with('error', $this->service->errors()->all());

        }
        return \Response::make($result['output'], 200, $result['headers']);
    }

    public function getService() {
        if(is_null($this->service)){
            $this->service = new BackOrderService();
        }
        return $this->service;
    }

    /**
     * 显示订单列表
     * @param  [type] $orderId [description]
     * @return [type]          [description]
     */
    public function doOrderShow()
    {
        if( !Input::has('status') )
        {
            $_GET['status'] = 1;
        }

        $arrInput=Input::All();
        
        $orderList = $this->getService()->getAllOrderTodayInfo($arrInput);

        $types = $_GET['status'];

        // dd(self::$view_path.'.'.self::$model_name);
        return View::make(self::$view_path.'.'.self::$model_name.'.lists', compact('orderList', 'types'));
    }

    /**
     * 获取today今日订单
     * 此功能用于出菜
     * @return
     */
    public function getOrderToday(){
        $arrInput=Input::All();
        return $this->getService()->getAllOrderTodayInfo($arrInput);
    }
    /**
     * 修改Today状态
     * 返回 1 或者 0
     */
    public function changeStatus(){
        $arrInput=Input::All();
        return $this->getService()->updateStatus($arrInput);
    }
    /**
     * 清空orderToday表
     */
    public function deleteOrderToday(){
        OrderToday::truncate();
    }
}
